import 'package:flutter/material.dart';
import 'package:lib_ui/widget/appbar_widget.dart';
import 'package:module_home/const/string.dart';
import 'package:module_home/model/TransferDataEntity.dart';
import 'package:module_home/page/iss_page.dart';
import 'package:module_home/viewmodel/ISSPageModel.dart';
import 'package:lib_core/widget/provider_widget.dart';
import 'package:lib_navigator/custom_route.dart';

/// 首页
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  int iss = 0;
  int ais = 0;
  IssPageModel model;

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return ProviderWidget<IssPageModel>(
        model: IssPageModel(),
        onModelInit: (model) {
          model.loadData();
        },
        builder: (context, model, child) {
          this.model = model;
          return _scrollView();
        });
  }

  Widget _scrollView() {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: appBar(
          home_title,
          showBack: false,
        ),
        body: Scrollbar(
          child: SingleChildScrollView(
            child: Column(
              children: [
                Padding(
                  padding: EdgeInsets.only(top: 60),
                  child: Text(s_version,
                      style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                          color: Colors.black)),
                ),
                Padding(
                    padding: EdgeInsets.only(top: 90),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        _buildTop(iss.toString() + minute, s_iss),
                        Padding(
                          padding: EdgeInsets.only(left: 20),
                          child: _buildTop(ais.toString() + minute, s_ais),
                        ),
                      ],
                    )),
                Container(
                  margin: EdgeInsets.only(top: 20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        severity_illness,
                        style: TextStyle(color: Colors.black, fontSize: 18),
                      ),
                      Padding(
                          padding: EdgeInsets.only(left: 10),
                          child: ClipRRect(
                              borderRadius: BorderRadius.circular(8),
                              // 圆角半径
                              child: _buildText()))
                    ],
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 40),
                  child: Container(
                    width: 200,
                    height: 48,
                    child: ElevatedButton(
                      style: ButtonStyle(
                          shape: MaterialStateProperty.all(StadiumBorder(
                              side: BorderSide(
                        style: BorderStyle.none,
                      )))),
                      child: Text(start_grading),
                      onPressed: () {
                        _toTransferForResult();
                        // toPage(ISSSortPage());
                      },
                    ),
                  ),
                ),
                Padding(
                    padding: EdgeInsets.only(top: 100),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: _buildBottom(),
                    ))
              ],
            ),
          ),
        ));
  }

  Widget _buildText() {
    String str;
    Color color;
    if (iss == 0) {
      str = none;
      color = Colors.grey;
    }
    if (0 < iss && iss < 16) {
      str = minor_injuries;
      color = Colors.green;
    }
    if (iss >= 16 && iss <= 25) {
      str = seriously_injured;
      color = Colors.pink[300];
    }

    if (iss >= 25) {
      str = serious_injury;
      color = Colors.orange;
    }

    return Container(
        alignment: Alignment.center,
        width: 80,
        height: 30,
        color: color,
        child: Text(str, style: TextStyle(color: Colors.white, fontSize: 16)));
  }

  Widget _buildTop(String score, String str) {
    return Stack(
      children: [
        Image.asset(
          "images/iss_img.png",
          width: 120,
          height: 120,
        ),
        Container(
          width: 120,
          height: 120,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                score,
                style: TextStyle(
                    fontSize: 18,
                    color: Colors.black,
                    fontWeight: FontWeight.bold),
              ),
              Text(
                str,
                style: TextStyle(fontSize: 13),
              )
            ],
          ),
        )
      ],
    );
  }

  List<Widget> _buildBottom() {
    List<String> items = [];
    items.add(s_rule);
    items.add(s_rule_1);
    items.add(s_rule_2);
    items.add(s_rule_3);
    items.add(s_rule_4);
    return items.map((item) {
      return Container(
          margin: EdgeInsets.only(right: 10, left: 10),
          padding: EdgeInsets.only(bottom: 8),
          child: Text(
            item,
            softWrap: true,
            style: TextStyle(
                color: getColor(item),
                fontSize: item.contains(rule) ? 18 : 16,
                fontWeight:
                    item.contains(rule) ? FontWeight.bold : FontWeight.normal),
          ));
    }).toList();
  }

  Color getColor(String item) {
    if (item.contains(s_2005)) {
      return Colors.red;
    } else if (item.contains(rule)) {
      return Colors.black;
    }
    return Colors.black54;
  }

  ///跳转的时候我们需要使用异步等待回调结果 dataFromOtherPage 就是返回的结果
  _toTransferForResult() async {
    final dataFromOtherPage = await Navigator.push(
      context,
      CustomRoute(ISSSortPage(
        list: model.list,
        issDataList: model.issDataList,
      )),
    ) as TransferDataEntity;
    if (dataFromOtherPage == null) return;
    setState(() {
      iss = dataFromOtherPage.ssValue;
      ais = dataFromOtherPage.aisValue;
      model.loadData();
    });
  }

  @override
  bool get wantKeepAlive => true;
}
